

// 新增班级
$('#addClassesBtn').click(function () {
    // 获取用户选中专业的 _id
    const subjectsId = $('#subjectsId').val();
    const name = $('#classesName').val();
    $.ajax({
        url: '/classes/createClasses',
        type: 'POST',
        data: { subjectsId, name },
        success(res) {
            console.log('新增班级的结果', res);
        }
    })
})

showClasses();

async function showClasses() {
    const classesData = await getClassesData();
    classesTableRender(classesData);
}

function getClassesData() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/classes/getClasses',
            success(res) {
                if (res.code) {
                    resolve(res.result);
                }
            }
        })
    })
}

function classesTableRender(data) {
    const classesHtml = data.map(item => `
        <tr>
            <td>${item._id}</td>
            <td>${item.subjectsId.name}</td>
            <td>${item.name}</td>
            <td>
                <a href="#">修改</a>
                <a href="#">删除</a>
            </td>
        </tr>
    `).join('');
    $('#classesTb').html(classesHtml);
}

function getClassesBySubjectsId(subjectsId) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/classes/getClassesBySubjectsId',
            type: 'GET',
            data: {
                subjectsId
            },
            success(res) {
                if (res.code) {
                    const classesHtml = res.result.map(item => `<option value="${item._id}">${item.name}</option>`).join('');
                    // 新增学生
                    $('#studentClass').html(classesHtml);
                    // 修改学生
                    $('#updateClassesId').html(classesHtml);
                    resolve(true);
                }
            }
        })
    })
}